package com.viphuli.app.tool.fragment;

import android.widget.TimePicker;
import butterknife.Bind;
import butterknife.OnClick;

import com.loopj.android.http.RequestParams;
import com.viphuli.app.tool.R;
import com.viphuli.app.tool.bean.part.AccountRemind;
import com.viphuli.app.tool.common.AccessTokenKeeper;
import com.viphuli.app.tool.handler.MyBaseHttpResponseHandler.HandlerType;
import com.viphuli.app.tool.http.ApiRequest;

public class AccountRemindTimeDialog extends BaseProgressBottomDialogFragment {

	@Bind(R.id.id_account_remind_time_pick)
	protected TimePicker timePick;
	private AccountRemind remind;

	@Override
	protected int obtainContentRes() {
		return R.layout.frag_account_remind_time_dialog;
	}

	@Override
	protected void initParams() {

	}

	public void setRemind(AccountRemind remind) {
		this.remind = remind;
	}

	@Override
	protected void initView() {

		timePick.setIs24HourView(true);

	}

	@Override
	protected void initData() {
		if (remind != null) {
			int minutes = remind.getAheadMinutes();
			int hour = minutes / 60;
			int minute = minutes % 60;
			timePick.setCurrentHour(hour);     //设置当前小时
			timePick.setCurrentMinute(minute); //设置当前分钟
		}
	}

	@OnClick(R.id.id_btn_cacel)
	public void clickCancel() {
		dismiss();
	}

	@OnClick(R.id.id_btn_enter)
	public void clickEnter() {

		AccountRemindFragment remindFrag = (AccountRemindFragment) getParentFragment();

		int hour = timePick.getCurrentHour();
		int minute = timePick.getCurrentMinute();

		remindFrag.setHour(hour);
		remindFrag.setMinute(minute);
		int minutes = hour * 60 + minute;
		remind.setAheadMinutes(minutes);

		remindFrag.getTimeDescText().setText("提前" + hour + "小时" + minute + "分");

		// 设置请求
		if (hour > 0 || minute > 0) {
			AccessTokenKeeper tokenKeeper = AccessTokenKeeper
					.readAccessToken(getActivity());

			boolean switching = false;
			String msg = "";

			AccountRemind remind = remindFrag.getRemind();
			if (remind != null) {
				switching = remind.isSwitching() ? true : false;
				msg = remind.getMsg();
			}

			if (tokenKeeper.isLogin() || remind != null) {


				RequestParams params = new RequestParams();
				params.put("uid", tokenKeeper.getOpenId());
				params.put("switch", switching);
				params.put("repeat", false);
				params.put("ahead_minutes", minutes);
				params.put("msg", msg);

				ApiRequest.accountChangeRemindInfo.request(this, params,
						HandlerType.none);

			}
		}

		dismiss();
	}

}
